home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / devtools / rpcgen / rpc_sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  7.2 KB  |  259 lines

  1. /*
  2.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3.  * unrestricted use provided that this legend is included on all tape
  4.  * media and as a part of the software program in whole or part.  Users
  5.  * may copy or modify Sun RPC without charge, but are not authorized
  6.  * to license or distribute it to anyone else except as part of a product or
  7.  * program developed by the user or with the express written consent of
  8.  * Sun Microsystems, Inc.
  9.  *
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  *
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  *
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  *
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  *
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30.  
  31. #ifndef lint
  32. static char sccsid[] = "@(#)rpc_sample.c  1.1  90/08/30  (C) 1987 SMI";
  33.  
  34. #endif
  35.  
  36. /*
  37.  * rpc_sample.c, Sample client-server code outputter for the RPC protocol compiler
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "rpc_parse.h"
  43. #include "rpc_util.h"
  44.  
  45. #ifdef __STDC__
  46. #include "rpc_scan.h"
  47. #include "protos.h"
  48.  
  49. void write_sample_svc ( definition *def );
  50. int write_sample_clnt ( definition *def );
  51. static void write_sample_client ( char *program_name , version_list *vp );
  52. static void write_sample_server ( definition *def );
  53. static void return_type ( proc_list *plist );
  54. void add_sample_msg ( void );
  55. void write_sample_clnt_main ( void );
  56. #endif
  57.  
  58. static char RQSTP[] = "rqstp";
  59.  
  60. void
  61. write_sample_svc(definition *def)
  62. {
  63.  
  64.     if (def->def_kind != DEF_PROGRAM) 
  65.       return;
  66.     write_sample_server(def);
  67. }
  68.  
  69.  
  70. int
  71. write_sample_clnt(definition *def)
  72. {
  73.         version_list *vp;
  74.     int count = 0;
  75.  
  76.     if (def->def_kind != DEF_PROGRAM) 
  77.       return( 0 );
  78.     /* generate sample code for each version */
  79.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  80.       write_sample_client(def->def_name, vp );
  81.       ++count;
  82.     }
  83.     return( count );
  84. }
  85.  
  86.  
  87. static void
  88. write_sample_client(char *program_name, version_list *vp)
  89. {
  90.   proc_list *proc;
  91.   int i;
  92.   decl_list *l;
  93.  
  94.   f_print(fout, "\n\nvoid\n" );
  95.   pvname( program_name, vp->vers_num );
  96.   if( Cflag )
  97.     f_print(fout,"( char* host )\n{\n" );
  98.   else
  99.     f_print(fout, "(host)\nchar *host;\n{\n" );
  100.   f_print(fout, "\tCLIENT *clnt;\n");
  101.  
  102.   i = 0;
  103.   for (proc = vp->procs; proc != NULL; proc = proc->next) {
  104.       f_print(fout, "\t");
  105.       ptype(proc->res_prefix, proc->res_type, 1);
  106.       f_print(fout, " *result_%d;\n",++i);
  107.       /* print out declarations for arguments */
  108.       if( proc->arg_num < 2 && !newstyle) {
  109.     f_print( fout, "\t" );
  110.     if( !streq( proc->args.decls->decl.type, "void") )
  111.       ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 1);
  112.     else
  113.       f_print(fout, "char* ");  /* cannot have "void" type */
  114.     f_print(fout, " ");
  115.     pvname( proc->proc_name, vp->vers_num );
  116.     f_print(fout, "_arg;\n");
  117.       } else if (!streq( proc->args.decls->decl.type, "void")) {
  118.     for (l = proc->args.decls; l != NULL; l = l->next) {
  119.       f_print( fout, "\t" );
  120.       ptype(l->decl.prefix, l->decl.type, 1);
  121.       f_print( fout, " ");
  122.       pvname( proc->proc_name, vp->vers_num );
  123.       f_print(fout, "_%s;\n", l->decl.name );
  124. /*      pdeclaration(proc->args.argname, &l->decl, 1, ";\n" );*/
  125.     }
  126.       }
  127.     }
  128.  
  129.   /* generate creation of client handle */
  130.   f_print(fout, "\tclnt = clnt_create(host, %s, %s, \"%s\");\n", 
  131.       program_name, vp->vers_name, tirpcflag? "netpath" : "udp");
  132.   f_print(fout, "\tif (clnt == NULL) {\n");
  133.   f_print(fout, "\t\tclnt_pcreateerror(host);\n");
  134.   f_print(fout, "\t\texit(1);\n\t}\n");
  135.  
  136.   /* generate calls to procedures */
  137.   i = 0;
  138.   for (proc = vp->procs; proc != NULL; proc = proc->next) {
  139.       f_print(fout, "\tresult_%d = ",++i);      
  140.       pvname(proc->proc_name, vp->vers_num);
  141.       if (proc->arg_num < 2 && !newstyle) {
  142.     f_print(fout, "(" );
  143.     if( streq( proc->args.decls->decl.type, "void") )  /* cast to void* */
  144.       f_print(fout, "(void*)");
  145.     f_print(fout, "&" );
  146.     pvname(proc->proc_name, vp->vers_num );
  147.     f_print(fout, "_arg, clnt);\n");
  148.       } else if (streq( proc->args.decls->decl.type, "void")) {
  149.         f_print(fout, "(clnt);\n");
  150.       }
  151.       else {
  152.     f_print(fout, "(");
  153.     for (l = proc->args.decls;  l != NULL; l = l->next) {
  154.       pvname( proc->proc_name, vp->vers_num );
  155.       f_print(fout, "_%s, ", l->decl.name);
  156.     }
  157.     f_print(fout, "clnt);\n");
  158.       }
  159.       f_print(fout, "\tif (result_%d == NULL) {\n", i);
  160.       f_print(fout, "\t\tclnt_perror(clnt, \"call failed:\");\n");
  161.       f_print(fout, "\t}\n");
  162.     }
  163.  
  164.   f_print(fout, "\tclnt_destroy( clnt );\n" );
  165.   f_print(fout, "}\n");
  166. }
  167.  
  168. static void
  169. write_sample_server(definition *def)
  170. {
  171.     version_list *vp;
  172.     proc_list *proc;
  173.  
  174.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  175.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  176.             f_print(fout, "\n");
  177. /*            if( Cflag )
  178.               f_print( fout, "extern \"C\"{\n");
  179. */
  180.             return_type(proc);
  181.             f_print(fout, "* \n");
  182.             if( Cflag )
  183.               pvname_svc(proc->proc_name, vp->vers_num);
  184.             else
  185.               pvname(proc->proc_name, vp->vers_num);
  186.             printarglist( proc, RQSTP, "struct svc_req *" );
  187.  
  188.             f_print(fout, "{\n");
  189.             f_print(fout, "\n\tstatic ");
  190.             if( !streq( proc->res_type, "void") )
  191.               return_type(proc);
  192.             else
  193.               f_print(fout, "char*" );  /* cannot have void type */
  194.             f_print(fout, " result;\n", proc->res_type);
  195.             f_print(fout, 
  196.                 "\n\t/*\n\t * insert server code here\n\t */\n\n");
  197.             if( !streq( proc->res_type, "void") )
  198.               f_print(fout, "\treturn(&result);\n}\n");
  199.             else  /* cast back to void * */
  200.               f_print(fout, "\treturn((void*) &result);\n}\n"); 
  201. /*            if( Cflag)
  202.               f_print( fout, "};\n");
  203. */
  204.  
  205.         }         
  206.     }
  207. }
  208.  
  209.  
  210.  
  211. static  void
  212. return_type(proc_list *plist)
  213. {
  214.   ptype( plist->res_prefix, plist->res_type, 1 );
  215. }
  216.  
  217. void
  218. add_sample_msg(void)
  219. {
  220.     f_print(fout, "/*\n");
  221.     f_print(fout, " * This is sample code generated by rpcgen.\n");
  222.     f_print(fout, " * These are only templates and you can use them\n");
  223.     f_print(fout, " * as a guideline for developing your own functions.\n");
  224.     f_print(fout, " */\n\n");
  225. }
  226.  
  227. void
  228. write_sample_clnt_main(void)
  229. {
  230.   list *l;
  231.   definition *def;
  232.   version_list *vp;
  233.  
  234.   f_print(fout, "\n\n" );
  235.   if( Cflag )
  236.     f_print(fout,"main( int argc, char* argv[] )\n{\n" );
  237.   else
  238.     f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n{\n" );
  239.  
  240.   f_print(fout, "\tchar *host;");
  241.   f_print(fout, "\n\n\tif(argc < 2) {");
  242.   f_print(fout, "\n\t\tprintf(\"usage: %%s server_host\\n\", argv[0]);\n" );
  243.   f_print(fout, "\t\texit(1);\n\t}");
  244.   f_print(fout, "\n\thost = argv[1];\n");
  245.  
  246.   for (l = defined; l != NULL; l = l->next) {
  247.         def = l->val;
  248.         if (def->def_kind != DEF_PROGRAM) {
  249.             continue;
  250.         }
  251.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  252.                 f_print( fout, "\t" );
  253.             pvname(def->def_name, vp->vers_num);
  254.             f_print( fout, "( host );\n" );
  255.               }
  256.         }
  257.   f_print(fout, "}\n");
  258. }
  259.